<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:deviceStatus:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:deviceStatus:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="printBox"
          v-hasPermi="['equipment:deviceStatus:print']"
          >打印</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="流程状态" width="100">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        label="设备名称"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.deviceName
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        label="设备编号"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.deviceCode
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        width="120"
        label="申请部门"
        prop="deptName"
      />

      <el-table-column show-overflow-tooltip label="出厂编号" prop="code" />
      <el-table-column
        show-overflow-tooltip
        label="恢复使用前检定/校准日期"
        prop="checkDate"
        min-width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.checkDate || "--" }}
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="申请内容" prop="code">
        <template slot-scope="scope">
          <!-- 1-停用 2-报废 3-恢复使用 -->
          {{
            scope.row.statusType == 1
              ? "停用"
              : scope.row.statusType == 2
              ? "报废"
              : scope.row.statusType == 3
              ? "恢复使用"
              : "维修"
          }}
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="创建时间"
        align="center"
        min-width="200"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['equipment:deviceStatus:info']"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 'refuse'"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:deviceStatus:edit']"
            >修改</el-button
          > -->
          <el-button
            v-if="scope.row.statusType == 4"
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['equipment:deviceStatus:log']"
            @click="handleRecord(scope.row)"
            >检测证书上传记录</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 打印模板位置 -->
    <PrintBox
      :pageNum="queryParams.pageNum"
      :total="total"
      :printRow="printList"
      :isVertical="false"
      :isPageSize="false"
      id="printBox"
    >
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 40px">
          设备停用/报废/恢复使用审批表 STET/B/20-3
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table
          style="border-collapse: collapse; table-layout: fixed; width: 90%"
          border="1"
          cellspacing="0"
          class="table-page"
          :isVertical="false"
        >
          <tr class="table_header" style="height: 20mm">
            <td colspan="2" style="width: 40mm">设备名称</td>
            <td colspan="4" style="width: 80mm">{{ scope.item.deviceName }}</td>
            <td colspan="2" style="width: 40mm">规格型号</td>
            <td colspan="3" style="width: 80mm">
              {{ scope.item.spec }}
            </td>
          </tr>
          <tr class="table_header" style="height: 20mm">
            <td colspan="2">出厂编号</td>
            <td colspan="4">{{ scope.item.code }}</td>
            <td colspan="2">设备编号</td>
            <td colspan="3">
              {{ scope.item.deviceCode }}
            </td>
          </tr>
          <tr style="height: 20mm">
            <td colspan="2">申请内容</td>
            <td colspan="9">
              <div style="display: flex; align-items: center">
                <div style="display: flex; align-items: center; width: 20mm">
                  <input
                    type="checkbox"
                    :checked="scope.item.statusType == 1"
                  />停用
                </div>
                <div style="display: flex; align-items: center; width: 20mm">
                  <input
                    type="checkbox"
                    :checked="scope.item.statusType == 2"
                  />报废
                </div>
                <div style="display: flex; align-items: center; width: 20mm">
                  <input
                    type="checkbox"
                    :checked="scope.item.statusType == 3"
                  />恢复使用
                </div>
              </div>
            </td>
          </tr>
          <tr style="height: 20mm">
            <td colspan="2">恢复使用前检定/校准日期</td>
            <td colspan="9">{{ scope.item.checkDate }}</td>
          </tr>
          <tr>
            <td colspan="2">申请理由</td>

            <td colspan="9" style="height: 60mm">
              <div>
                <div
                  v-for="item in scope.item.contentList"
                  style="display: flex; align-items: center"
                >
                  <input type="checkbox" :checked="item.isSelected == 0" />{{
                    item.contentLabel
                  }}
                </div>
              </div>
              <div style="display: flex; align-items: center; margin-top: 20mm">
                <div style="display: flex; align-items: center; width: 40mm">
                  申请部门：{{ scope.item.deptName }}
                </div>
                <div style="width: 60mm; position: relative">
                  申请人：
                  <img
                    style="width: 80px; position: absolute; top: -10px"
                    :src="$VUE_APP_IMG_API + scope.item.esign"
                  />
                  <!-- {{ scope.item.createByName }} -->
                </div>
                <div style="display: flex; align-items: center; width: 50mm">
                  日期：{{ parseTime(scope.item.createTime, "{y}-{m}-{d}") }}
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 20mm;
                "
              >
                <div style="width: 60mm; position: relative">
                  检测部门负责人：<template
                    v-if="CheckApprove(scope, 'jcbmfzr').ShowImg"
                  >
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'jcbmfzr').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "jcbmfzr").endTime }}
                </div>
                <div style="width: 10mm"></div>
              </div>
            </td>
          </tr>

          <tr>
            <td colspan="2">设备管理员</td>

            <td colspan="9" style="height: 70mm">
              <div
                style="
                  display: flex;
                  align-items: center;
                  margin-top: 100px;
                  position: relative;
                "
              >
                <div style="display: flex; align-items: center; width: 20mm">
                  <input
                    type="checkbox"
                    :checked="checkHistoryNodeComment(scope, 'sbgly').type == 1"
                  />情况属实
                </div>
                <div
                  style="
                    display: flex;
                    align-items: center;
                    width: 30mm;
                    margin-left: 15mm;
                  "
                >
                  <input
                    type="checkbox"
                    :checked="
                      checkHistoryNodeComment(scope, 'sbgly').type == 2 ||
                      checkHistoryNodeComment(scope, 'sbgly').type == 3
                    "
                  />情况不符
                </div>
                <div style="position: absolute; text-align: left; top: 20px">
                  <template v-if="CheckApprove(scope, 'sbgly').ShowMessage">
                    {{ CheckApprove(scope, "sbgly").MessageText }}
                  </template>
                </div>
              </div>

              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 100px;
                "
              >
                <div style="width: 60mm; position: relative">
                  签字：<template v-if="CheckApprove(scope, 'sbgly').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'sbgly').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "sbgly").endTime }}
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2">质量负责人</td>

            <td colspan="9" style="height: 70mm">
              <div
                style="
                  display: flex;
                  align-items: center;
                  margin-top: 100px;
                  position: relative;
                "
              >
                <div style="display: flex; align-items: center; width: 20mm">
                  <input
                    type="checkbox"
                    :checked="checkHistoryNodeComment(scope, 'zlfzr').type == 1"
                  />同意
                </div>
                <div
                  style="
                    display: flex;
                    align-items: center;
                    width: 20mm;
                    margin-left: 15mm;
                  "
                >
                  <input
                    type="checkbox"
                    :checked="
                      checkHistoryNodeComment(scope, 'zlfzr').type == 2 ||
                      checkHistoryNodeComment(scope, 'zlfzr').type == 3
                    "
                  />不同意
                </div>
                <div style="position: absolute; text-align: left; top: 20px">
                  <template v-if="CheckApprove(scope, 'zlfzr').ShowMessage">
                    {{ CheckApprove(scope, "zlfzr").MessageText }}
                  </template>
                </div>
              </div>
              <div
                style="
                  display: flex;
                  justify-content: flex-end;
                  text-align: left;
                  margin-top: 100px;
                "
              >
                <div style="width: 60mm; position: relative">
                  签字：<template v-if="CheckApprove(scope, 'zlfzr').ShowImg">
                    <img
                      style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'zlfzr').ApproveImg"
                    />
                  </template>
                </div>
                <div style="width: 40mm">
                  日期：{{ checkHistoryNode(scope, "zlfzr").endTime }}
                </div>
              </div>
            </td>
          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import {
  listDeviceStatus,
  delDeviceStatus,
} from "@/api/device/deviceStatus.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "deviceStatus",
  dicts: ["sys_device_sq_content"],

  data() {
    return {
      infos: [], //勾选的数据
      printList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null,
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = "设备停用/报废/恢复使用审批表 STET/B/20-3";

        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          },
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          padding: 4px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceStatusId);
      this.single = selection.length != 1;
      this.infos = selection;

      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.deviceStatusId || this.ids;
      this.$router.push("./addDeviceStatus?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      // this.$router.push(
      //   "./addDeviceStatus?isInfo=true&id=" + row.deviceStatusId
      // );
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));


      this.$router.push({
        path: "/deviceStatus/process/detail/" + row.piid,
        query: {
          definitionId: null,
          deployId: null,
          taskId: null,
          finished: false,
          id: row.deviceStatusId,
        },
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.deviceStatusId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "deviceStatusId",
        "DELETE",
        "deviceName"
      );
      this.$modal
        .confirm('是否确认删除设备名称为"' + name + '"的数据项？')
        .then(function () {
          return delDeviceStatus(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleRecord(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));

      const configId = row.deviceStatusId || this.ids;
      let e = this.dataList.find((i) => i.deviceStatusId == configId);
      this.$router.push(
        "./deviceCheckCert?isWx=true&id=" + configId + "&deviceId=" + e.deviceId
      );
    },
    handleAdd() {
      this.$router.push("./addDeviceStatus");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;

      listDeviceStatus(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/deviceStatus/export",
        {
          ...this.queryParams,
        },
        `设备停用/报废/恢复使用审批记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
